﻿using System;
using System.Diagnostics;

namespace CsharpTool.Common
{
    public class ProcessTool
    {
        /// <summary>
        /// 执行Linux命令
        /// </summary>
        /// <param name="cmd">具体命令</param>
        /// <returns>item1: 标准输出; item2: 标准错误输出</returns>
        public static (string, string) ProcessLinuxCmd(string cmd)
        {
            var process = new Process();
            process.StartInfo.FileName = "/bin/bash";
            process.StartInfo.Arguments = $"-c \"{cmd}\"";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            var output = process.StandardOutput.ReadToEnd();
            var error = process.StandardError.ReadToEnd();
            process.WaitForExit();
            process.Close();
            process.Dispose();
            return (output, error);
        }

        /// <summary>
        /// 执行windows powershell命令
        /// </summary>
        /// <returns>item1: 标准输出; item2: 标准错误输出</returns>
        public static (string, string) ProcessWinPowerShellCmd(string cmd)
        {
            var startInfo = new ProcessStartInfo
            {
                FileName = "powershell.exe",
                RedirectStandardInput = true,
                RedirectStandardOutput = true,
                RedirectStandardError = true,
                UseShellExecute = false,
                CreateNoWindow = true
            };
            var output = "";
            var error = "";
            using (var process = new Process { StartInfo = startInfo })
            {
                process.Start();
                process.StandardInput.WriteLine(cmd);
                process.StandardInput.WriteLine("Exit");
                output = process.StandardOutput.ReadToEnd();
                error = process.StandardError.ReadToEnd();
                process.WaitForExit();
            }

            var splits = output.Split(Environment.NewLine, StringSplitOptions.RemoveEmptyEntries);
            output = string.Join(Environment.NewLine, splits, 4, splits.Length - 5);
            return (output, error);
        }

        /// <summary>
        /// 执行windows cmd命令
        /// </summary>
        /// <returns>item1: 标准输出; item2: 标准错误输出</returns>
        public static (string, string) ProcessWinCmdCmd(string cmd)
        {
            var process = new Process();
            process.StartInfo.FileName = "cmd.exe";
            process.StartInfo.Arguments = $"/C \"{cmd}\"";
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;
            process.Start();
            var output = process.StandardOutput.ReadToEnd();
            var error = process.StandardError.ReadToEnd();
            process.WaitForExit();
            process.Close();
            process.Dispose();
            return (output, error);
        }
    }
}
